package com.wsj.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.dao.DataAccessException;

import com.wsj.dao.IHomeSiteNewsClassDao;
import com.wsj.dao.IHomeSiteNewsDao;
import com.wsj.service.IHomeSiteNewsServ;
import com.wsj.util.exception.BusinessException;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.tool.Tool;
import com.wsj.web.constant.homeSiteNews.IndexPageContent;
import com.wsj.web.subclass.displayBean.HomeSiteNews.AutoPlayFormExtendBean;
import com.wsj.web.subclass.displayBean.HomeSiteNews.HomeSiteIndexPageColumnBean;
import com.wsj.web.subclass.displayBean.HomeSiteNews.HomeSiteNewsExtendBean;
import com.wsj.web.subclass.displayBean.HomeSiteNews.HomeSiteNewsFormExtendBean;
import com.wsj.web.vo.homeSite.AutoPlay;
import com.wsj.web.vo.homeSite.HomeSiteNews;
import com.wsj.web.vo.homeSite.HomeSiteNewsClass;

public class HomeSiteNewsServImpl implements IHomeSiteNewsServ {
	private IHomeSiteNewsDao homeSiteNewsDaoImpl;
	
	private IHomeSiteNewsClassDao  homeSiteNewsClassDao;
	

	public IHomeSiteNewsClassDao getHomeSiteNewsClassDao() {
		return homeSiteNewsClassDao;
	}

	public void setHomeSiteNewsClassDao(IHomeSiteNewsClassDao homeSiteNewsClassDao) {
		this.homeSiteNewsClassDao = homeSiteNewsClassDao;
	}

	public boolean add(HomeSiteNews homeSiteNews) throws BusinessException {
		this.homeSiteNewsDaoImpl.add(homeSiteNews);
		return true;
		
	}

	public boolean delete(long id) throws BusinessException {
		this.homeSiteNewsDaoImpl.delete(id);
		return true;
	}

	public HomeSiteNews getHomeSiteNewsById(long id) throws BusinessException {
		HomeSiteNews homeSiteNews=null;
		homeSiteNews=this.homeSiteNewsDaoImpl.getHomeSiteNewsById(id);
		return homeSiteNews;
	}

	public HomeSiteNews getHomeSiteNewsByName(Long homeSiteNewsClassid,String title)
			throws BusinessException {
		HomeSiteNews homeSiteNews=null;
		homeSiteNews=this.homeSiteNewsDaoImpl.getHomeSiteNewsByName(homeSiteNewsClassid,title);
		return homeSiteNews;
	}

	public PageResultSet loadHomeSiteNewsByParam(String keyword, Integer qyId,
			Integer iscommend, long personalId, String title, Integer provId,
			Integer cityId, Integer diquId, int page, int pageSize,
			long homeSiteNewsClassid,short isDel,String  content,String  createDatetimeStr)
			throws BusinessException {
		PageResultSet pageResultSet=null;
		
		pageResultSet=this.homeSiteNewsDaoImpl.loadHomeSiteNewsByParam(keyword, qyId, iscommend, personalId, title, provId, 
				cityId, diquId, page, pageSize,homeSiteNewsClassid,isDel,content, createDatetimeStr);
		
		return pageResultSet;
	}

	public boolean update(HomeSiteNews homeSiteNews) throws BusinessException {
		
		this.homeSiteNewsDaoImpl.update(homeSiteNews);
		return true;

	}
	
	/** 根据新闻 ID，新闻点击率 加一 */
	public void updateHits(Long homeSiteNewsId) throws BusinessException {
		HomeSiteNews hs = this.homeSiteNewsDaoImpl.getHomeSiteNewsById(homeSiteNewsId);
		hs.setHits(hs.getHits()+1);
		this.homeSiteNewsDaoImpl.update(hs);
	}

	public IHomeSiteNewsDao getHomeSiteNewsDaoImpl() {
		return homeSiteNewsDaoImpl;
	}

	public void setHomeSiteNewsDaoImpl(IHomeSiteNewsDao homeSiteNewsDaoImpl) {
		this.homeSiteNewsDaoImpl = homeSiteNewsDaoImpl;
	}

	public PageResultSet loadHomeSiteNewsByExtendBean(HomeSiteNewsFormExtendBean homeSiteNewsFormExtendBean) throws BusinessException {
		PageResultSet pageResultSet=null;
		pageResultSet=this.homeSiteNewsDaoImpl.loadHomeSiteNewsByExtendBean(homeSiteNewsFormExtendBean);
		
		return pageResultSet;
	}

	/*****
	 * GBX 加 搜索用 
	 * 条件： 标题、内容、关键字 、
	 * 附加条件：已审核、删除否、推荐、时间排序
	 * */  
	public PageResultSet loadNewsByParam(Integer type,String keyWord, Integer page, Integer pageSize) throws BusinessException {
		PageResultSet pageResultSet = null;
		String key = Tool.cutMultiSpace(keyWord);
		String[] keyList = key.split(" "); 
		pageResultSet = this.homeSiteNewsDaoImpl.loadNewsByParam(keyList,type, page, pageSize);			
		return pageResultSet;
	}

	public List getTopHSiteNewsListByNewsClassId(long newsClassId, int top) throws BusinessException {
		List list=this.homeSiteNewsDaoImpl.getTopHSiteNewsListByNewsClassId(newsClassId, top);
		
		return list;
	}

	public List getTopHSiteNByOnlyNewId(long newsClassId, int top) throws BusinessException {
		List list=this.homeSiteNewsDaoImpl.getTopHSiteNByOnlyNewId(newsClassId, top);
		return list;
	}

	/**
	 * 主要用于首页新闻资讯的查询
	 * 基本条件：删除否、推荐、审核
	 * 条件：是否是图片新闻、资讯分类ID、前 top 条
	 * 额外条件： 是否热点 （第一条显示的要是热点）
	 * not in rdNewsId 不在热点内的资讯,null 则不排除
	 * */
	@SuppressWarnings("unchecked")
	public List getTopByclassId(Short hotspot,Integer isPic,Long newsClassId, Long[] rdNewsId, Integer top) throws BusinessException {
		return this.homeSiteNewsDaoImpl.getTopByNewsClassId(hotspot,isPic,newsClassId,rdNewsId, top);
	}

	public List getTopBykeyword(String keyword, Integer top) throws BusinessException {
		List list=this.homeSiteNewsDaoImpl.getTopBykeyword(keyword, top);
		return list;
	}

	/*****
	 * 通过新闻分类,图片或文字 ,加载前几个文章列表.
	 */
	public List loadNewsByClassNameList(String name, Short picOrChar, Short isdel, Short audite, Short isCommend, Integer top) throws BusinessException 
	{
		return this.getHomeSiteNewsDaoImpl().loadNewsByClassNameListDao( name,  picOrChar,  isdel,  audite,  isCommend,  top);
		
	}

	public List getTopByclassId(Short hotspot, Integer provId, Integer top) throws BusinessException {
		return this.homeSiteNewsDaoImpl.getTopByclassId(hotspot,provId, top);
	}


	public List getTopByclassId(Short hotspot,Integer id1, Integer id2, Integer id3, Integer id4, Short isPic, Integer top) throws BusinessException {
		return this.homeSiteNewsDaoImpl.getTopByclassId(hotspot,id1, id2, id3, id4, isPic, top);
	}

	
	/**
	 * 资讯列表页面
	 * 条件:资讯分类ID,审核通过、未删除
	 * 排序：时间、推荐
	 * **/ 
	public PageResultSet loadByClassId(Long classId, Integer page, Integer pageSize) throws BusinessException {
		return this.homeSiteNewsDaoImpl.loadByClassId(classId, page, pageSize);
	}
	
	/**
	 * 根据省份ID 、城市ID 加载资讯
	 * */
	public PageResultSet loadByProvOrCityId(Integer qyId,Integer provId, Integer cityId,Long cid, Integer page, Integer pageSize) throws BusinessException {
		return this.homeSiteNewsDaoImpl.loadByProvOrCityId(qyId,provId, cityId, cid, page, pageSize);
	}

	/**
	 * 根据 点击率 获取 前 top 的新闻
	 * */  
	public List getTopByHits(Integer top) throws BusinessException {
		return this.homeSiteNewsDaoImpl.loadTopByHits(top);
	}
	
	
	public HomeSiteNews getTopHSiteNByOrderBytime(long newsClassId, Integer id,Integer top) throws BusinessException {
		HomeSiteNews homeSiteNews=this.homeSiteNewsDaoImpl.getTopHSiteNByOrderBytime(newsClassId,id, top);
		return homeSiteNews;
	}

	/****
	 * 获取分站前几个点击量最大的资讯
	 */
	public List<HomeSiteNews> getTopByHits(Integer provId,Integer cityId, Integer top) throws BusinessException 
	{
		List<HomeSiteNews> retList= this.getHomeSiteNewsDaoImpl().loadTopByHitsDao(provId,cityId, top);
		return retList;
	}

	/*****
	 * 获取分站某个分类下面的资讯列表
	 */
	public List<HomeSiteNews> getTopByclassId(Integer provId,Integer cityId, Short hotspot, Integer isPic, Long newsClassId, Integer top) throws BusinessException 
	{
		return this.getHomeSiteNewsDaoImpl().loadTopByclassIdDao(provId,cityId,  hotspot,  isPic,  newsClassId,  top);
	}


    /******
     * 获取分站市场行情的资讯列表
     */
	public List<HomeSiteNews> loadSCHQClassNewsList(Integer provId,Integer cityId, Short hot, Short pic, Long id, Integer index_top_fifteen) throws BusinessException 
	{
		return  this.getHomeSiteNewsDaoImpl().loadSCHQClassNewsListDao(provId,cityId,  hot,  pic,  id,  index_top_fifteen);
	}

	
	
	public List loadHomeSiteNews(HomeSiteNewsFormExtendBean exbean) throws BusinessException {
		// TODO Auto-generated method stub
		List list=this.homeSiteNewsDaoImpl.loadHomeSiteNews(exbean);
		
		return list;
	}

	public List loadHomeSiteNewsByProvOrCityAndClassIds(HomeSiteNewsExtendBean homeExtend) throws BusinessException {
		// TODO Auto-generated method stub
		List list=this.homeSiteNewsDaoImpl.loadHomeSiteNewsByProvOrCityAndClassIds(homeExtend);
		return list;
	}

	/*************
	 * 通过类目id,获取前几条推荐的新闻,如果不存在查询所有类目
	 */
	public List<HomeSiteNews> getCommendHomeSiteNewsListByClassId(Long id, int top) throws BusinessException {
		
		List<HomeSiteNews> ret = null;
		try {
			ret =  homeSiteNewsDaoImpl.getCommendHomeSiteNewsListByClassId(id,top);
			if(ret==null || ret.isEmpty()){
				ret = homeSiteNewsDaoImpl.getCommendHomeSiteNewsList(top);
			}
		} catch (DataAccessException e) {
			throw new BusinessException("通过类目id,获取前几条推荐的新闻,如果不存在查询所有类目, 数据库异常",e);
		}
		 
		return ret;
	}

	/*********
	 * 通过关键字,查询相关的前5条相关新闻,如果不存在查询所有类目
	 */
	public List<HomeSiteNews> getInterrelatedHomeSiteNewsList(String keyWords, int top5) throws BusinessException {
		
		List<HomeSiteNews> ret = null;
		try {
			ret =  homeSiteNewsDaoImpl.getInterrelatedHomeSiteNewsList(keyWords,top5);
			if(ret==null || ret.isEmpty()){
				homeSiteNewsDaoImpl.getHomeSiteNewsList(top5);
			}
		} catch (DataAccessException e) {
			throw new BusinessException("通过关键字,查询相关的前5条相关新闻,如果不存在查询所有类目, 数据库异常",e);
		}
		return ret;
		
	}

	
	/************
	 * 获取引导阅读的新闻,优先查询带图片的新闻,按时间倒序排列
	 * 
	 */
	public List<HomeSiteNews> getHotCommendNewsListByClassId(Short hot_news_yes, Integer commend_news_yes, Long id, int top) throws BusinessException {
		
		List<HomeSiteNews> hotCommendNewsList = null;
		
		try {
			hotCommendNewsList = homeSiteNewsDaoImpl.getHotCommendNewsListByClassId(hot_news_yes,  commend_news_yes,  id,  top);
		} catch (DataAccessException e) {
			throw new BusinessException("获取引导阅读的新闻,优先查询带图片的新闻,按时间倒序排列, 数据库异常",e);
		}
		
		
		return hotCommendNewsList;
		
	}

	
	/***********
	 * 获取图片新闻列表
	 */
	public List<HomeSiteNews> getTopPicNewsByclassId(Short hot_news_no, Integer commend_news_yes, Long id, int picNewsNum) throws BusinessException {
      List<HomeSiteNews> picNewsList = null;
		
		try {
			picNewsList = homeSiteNewsDaoImpl.getTopPicNewsByclassId(hot_news_no,  commend_news_yes,  id,  picNewsNum);
		} catch (DataAccessException e) {
			throw new BusinessException("获取图片新闻列表, 数据库异常",e);
		}
		
		
		return picNewsList;
	}

	/***********
	 * 获取前几条置顶新闻
	 */
	public List<HomeSiteNews> getTodayTopTitle(int top) throws BusinessException {
		
		List<HomeSiteNews>  homeSiteNewsList=null;
		
		
		homeSiteNewsList = this.homeSiteNewsDaoImpl.getTodayTopTitle(top);
		
		
		return homeSiteNewsList;
		
	}

	/******
	 * 一周推荐
	 */
	public List<HomeSiteNews> getCommentNewsCollectList(long classId, int top) throws BusinessException {
		
		List<HomeSiteNews>  homeSiteNewsList=null;
		
		try {
			homeSiteNewsList = this.homeSiteNewsDaoImpl.getCommentNewsCollectList(classId,top);
		} catch (DataAccessException e) {
			throw new BusinessException("getCommentNewsCollectList error",e);
		}
		
		return homeSiteNewsList;
		
	}
	
	
	
	/*********
	 * 获取该栏目下所有的新闻
	 * @param columnName
	 *        栏目名称
	 * @param textNewsNum
	 *        一般新闻个数
	 * @param picNewsNum
	 *        图片新闻个数
	 * @param displayGridNews
	 *        是否显示引导阅读新闻
	 *        
	 *        一般不会引导新闻和图片新闻一致 
	 * @return
	 */
	public HomeSiteIndexPageColumnBean getColumnNews(String columnName, int textNewsNum, int picNewsNum, boolean displayGridNews) {
		
		HomeSiteIndexPageColumnBean homeSiteIndexPageColumnBean = null;
		
		//查询文本新闻的时候,需要除开的图片新闻和引导新闻的id数组
		List<Long> expIdS=new ArrayList<Long>();
		
		//图片新闻列表
		List<HomeSiteNews> homeSitePicNewsList=null;
		
		
		//推荐的文字新闻列表
		List<HomeSiteNews> homeSiteNewsList=null;
		Long[] expId = null;
		
		try {
			
			HomeSiteNewsClass columnNewsClass = this.homeSiteNewsClassDao.loadNewsClassByNames(columnName);
			if(columnNewsClass!=null){
				homeSiteIndexPageColumnBean = new HomeSiteIndexPageColumnBean();
				homeSiteIndexPageColumnBean.setHomeSiteNewsClass(columnNewsClass);
				
				//是否显示引导新闻
				if(displayGridNews){
					//获取引导新闻  推荐 热点 审核
					List<HomeSiteNews> guideHomeSiteNewsList = this.getHotCommendNewsListByClassId(IndexPageContent.HOT_NEWS_YES,IndexPageContent.COMMEND_NEWS_YES,columnNewsClass.getId(),1);
					if(guideHomeSiteNewsList!=null && !guideHomeSiteNewsList.isEmpty()){
						HomeSiteNews  guideHomeSiteNews = guideHomeSiteNewsList.get(0);
						if(guideHomeSiteNews!=null){
							expIdS.add(guideHomeSiteNews.getId());	
						}
						homeSiteIndexPageColumnBean.setHomeSiteNews(guideHomeSiteNews);
					}
					
				}
				
				
				if(picNewsNum>0){
//					获取图片新闻
					homeSitePicNewsList = this.getTopPicNewsByclassId(IndexPageContent.HOT_NEWS_YES,IndexPageContent.COMMEND_NEWS_YES,columnNewsClass.getId(), picNewsNum);
					homeSiteIndexPageColumnBean.setHomeSitePicNewsList(homeSitePicNewsList);
				}
				
				if(homeSitePicNewsList!=null && !homeSitePicNewsList.isEmpty()){
					for(int j=0;j<homeSitePicNewsList.size();j++){
						
						HomeSiteNews homeSiteNews = (HomeSiteNews)homeSitePicNewsList.get(j);
						if(homeSiteNews!=null){
							expIdS.add(homeSiteNews.getId());
						}
					}
				}
				
				if(expIdS!=null && !expIdS.isEmpty()){
					Object[] obj =expIdS.toArray();
					if(obj!=null){
						expId = new Long[obj.length];
						for(int j=0;j<obj.length;j++){
							expId[j] = (Long)obj[j];
						}
					}
				}
				
				if(textNewsNum>0){
//					获取文本,推荐的新闻
					homeSiteNewsList =  this.getTopByclassId(IndexPageContent.HOT_NEWS_NO,IndexPageContent.COMMEND_NEWS_NO,columnNewsClass.getId(), expId, textNewsNum);
					homeSiteIndexPageColumnBean.setHomeSiteNewsList(homeSiteNewsList);
				}
				
			}
			
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return homeSiteIndexPageColumnBean;
		
	}

	/******
	 * 添加自动播放新闻
	 */
	public Boolean addAutoPlay(AutoPlay autoPlay) throws BusinessException {
		
		  boolean isok = false;
		  try {
			isok =  this.getHomeSiteNewsDaoImpl().addAutoPlay(autoPlay);
		} catch (DataAccessException e) {
			throw new BusinessException("addAutoPlay error",e);
			
		}
		
		return isok;
		  
	}

	
	/*********
	 * 通过id,获取自动播放新闻
	 */
	public AutoPlay getAutoPlayById(Integer id) throws BusinessException {
		
		AutoPlay autoPlay =null;
		
		try {
			autoPlay = this.getHomeSiteNewsDaoImpl().getAutoPlayById(id);
		} catch (DataAccessException e) {
			
			throw new BusinessException("getAutoPlayById error",e);
		}
		
		return autoPlay;
	}

	
	/*******
	 * 获取自动播放列表
	 */
	public PageResultSet loadAutoPlayList(
			AutoPlayFormExtendBean autoPlayFormExtendBean)
			throws BusinessException {
		
		PageResultSet pageResultSet=null;
		pageResultSet=this.homeSiteNewsDaoImpl.loadAutoPlayList(autoPlayFormExtendBean);
		
		return pageResultSet;
		 
	}

	/*********
	 * 更新自动播放模块
	 */
	public Boolean updateAutoPlay(AutoPlay autoPlay) throws BusinessException {
		
		boolean isok=false;
		
		try {
			isok = this.getHomeSiteNewsDaoImpl().updateAutoPlay(autoPlay);
		} catch (DataAccessException e) {
			throw new BusinessException("updateAutoPlay error",e);
		}
		
		return isok;
		
	}

	
	/******
	 * 通过id,删除自动播放对象
	 */
	public void delAutoPlayById(Integer id) throws BusinessException {
	    
		  this.getHomeSiteNewsDaoImpl().delAutoPlayById(id);
		  
	}

	
	/********
	 * 获取前几个自动播放新闻
	 */
	public List<AutoPlay> getAutoPlayNewsList(int top) throws BusinessException {
		
		 return this.getHomeSiteNewsDaoImpl().getAutoPlayNewsList(top);
		 
	}

	

	

	

}
